#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<Python.h>

int isOK(int c);
const char * normalizeQuery(const char * query);
char normalizeQueryChar(char c1, char c2);

int isOK(int c)
{
    if( 'a' <= c && c <= 'z') return c;
    else if( 'A' <= c && c <= 'Z') return c - 'A' + 'a';
    else if( '0' <= c && c <= '9') return c;
    else if( c == '\n') return c;
    else if( c == ' ') return c;    
    else if( c ==  EOF) return c;
    else if( c == '|') return c;
    return 0;
}


const char * normalizeQuery(const char * query)
{
    int len = strlen(query);
    char * answerQuery = malloc( ( len + 1 ) * sizeof(char) );
    char b,c,d;
    int i = 0;
    int k = 0;
    while( i < len)
    {
           c = query[i];
           d = isOK(c);
           if( d > 0)
           {
                answerQuery[k] = d;
                k++;
                i++;
           }
           else if( c == '"')
           {
                i ++;
               continue;            
           }
           else
           {
                i++;
                b = query[i];
                if( normalizeQueryChar(c,b) > 0 )
                {
                    answerQuery[k] = normalizeQueryChar(c,b);
                    i ++ ;
                    k ++;
                }
                else
                {
                    answerQuery[k] = ' ';
                    k++;
                }
                
           }
    }
    answerQuery[k] = 0;
    return answerQuery;
}


char normalizeQueryChar(char c1, char c2)
{
    if( c1 == -60 && c2 == - 103) return '!';
    else if( c1 == -61 && c2 == - 77) return '@';
    else if( c1 == -60 && c2 == - 123) return '?';
    else if( c1 == -59 && c2 == - 101) return '$';
    else if( c1 == -59 && c2 == - 126) return '%';
    else if( c1 == -59 && c2 == - 68) return '^';
    else if( c1 == -59 && c2 == - 70) return '&';
    else if( c1 == -60 && c2 == - 121) return '*';
    else if( c1 == -59 && c2 == - 124) return '(';
    else if( c1 == -60 && c2 == - 104) return '!'; //duże litery!
    else if( c1 == -61 && c2 == - 109) return '@';
    else if( c1 == -60 && c2 == - 124) return '?';
    else if( c1 == -59 && c2 == - 102) return '$';
    else if( c1 == -59 && c2 == - 127) return '%';
    else if( c1 == -59 && c2 == - 69) return '^';
    else if( c1 == -59 && c2 == - 71) return '&';
    else if( c1 == -60 && c2 == - 122) return '*';
    else if( c1 == -59 && c2 == - 125) return '(';    
    else if( c1 == '#' && c2 == '#') return '#';
    else if( c1 == -61 && c2 == -68) return '>';
    else return 0;
}


int main()
{
    printf("%s\n",normalizeQuery("produkcik|produkt|przetworek|przetwór|wyrobek|wyrób|wytworek|wytwór krajowy|nasz|państwowy|polski bruttko|brutto"));
}

static PyObject* _normalize(PyObject* self, PyObject* args)
{
   char* word;
   if (!PyArg_ParseTuple(args, "s", &word))
        return NULL;
   return Py_BuildValue("s",  normalizeQuery(word) ) ; 
}


static PyMethodDef HelloMethods[] =
{
     {"normalize", _normalize, METH_VARARGS, "Normalizes the query [eg. polish chars -> {!,@, ... etc. }]"},
     {NULL, NULL, 0, NULL}
};
 
PyMODINIT_FUNC initnormalizeQuery(void)
{
     (void) Py_InitModule("normalizeQuery", HelloMethods);
}
